package Boundary;

import Entity.ModulesGrades;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import Entity.ModulesGrades;

import static Control.Control.Add_Component;
import static Control.Control.json1;
/**
 * @author Group77
 * @version 4.0
 * @description display an operational page for adding modules
 */
public class ModulesAdd extends Overall{

    /**
     * initialize the Jframe
     *
     * @param number : the name of each frame
     */
    public ModulesAdd(int number) {
        super("Add");

        panel1.setLayout(new BorderLayout());
        panel1.setBorder(BorderFactory.createEmptyBorder());
        JButton button0 = new JButton("   Go back   ");
        button0.setBorder(BorderFactory.createRaisedBevelBorder());
        button0.setBackground(Color.cyan);
        button0.setFont(new Font("STSong", Font.BOLD, 25));
        JPanel empty1 = new JPanel();
        empty1.setPreferredSize(new Dimension(100, 10));
        panel1.add(button0, BorderLayout.WEST);
        panel1.add(empty1, BorderLayout.NORTH);
        Add_Component(jfr, gbl, panel1, gbc, 0, 0, 1, 1, 1, 0);


        button0.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jfr.setVisible(false);
                        new ModulesManagement(number);
                        jfr.dispose();
                    }
                }
        );

        JLabel label1 = new JLabel("Module Number:", JLabel.CENTER);
        label1.setFont(new Font("TimesRoman", Font.PLAIN, 24));
        JTextField tf1 = new JTextField(12);
        tf1.setFont(new Font("TimesRoman", Font.PLAIN, 24));
        Box b1 = Box.createHorizontalBox();
        b1.add(label1);
        b1.add(Box.createHorizontalStrut(10));
        b1.add(tf1);
        panel2.add(b1);
        Add_Component(jfr, gbl, panel2, gbc, 0, 2, 1, 1, 1, 0);

        JLabel label2 = new JLabel("Module Name   :", JLabel.CENTER);
        label2.setFont(new Font("TimesRoman", Font.PLAIN, 24));
        JTextField tf2 = new JTextField(12);
        tf2.setFont(new Font("TimesRoman", Font.PLAIN, 24));
        Box b2 = Box.createHorizontalBox();
        b2.add(label2);
        b2.add(Box.createHorizontalStrut(10));
        b2.add(tf2);
        panel3.add(b2);
        Add_Component(jfr, gbl, panel3, gbc, 0, 3, 1, 1, 1, 0);

        JLabel label3 = new JLabel("Credits             :", JLabel.CENTER);
        label3.setFont(new Font("TimesRoman", Font.PLAIN, 24));
        JTextField tf3 = new JTextField(12);
        tf3.setFont(new Font("TimesRoman", Font.PLAIN, 24));
        Box b3 = Box.createHorizontalBox();
        b3.add(label3);
        b3.add(Box.createHorizontalStrut(10));
        b3.add(tf3);
        panel4.add(b3);
        Add_Component(jfr, gbl, panel4, gbc, 0, 4, 1, 1, 1, 0);

        JLabel label4 = new JLabel("Grades             :", JLabel.CENTER);
        label4.setFont(new Font("TimesRoman", Font.PLAIN, 24));
        JTextField tf4 = new JTextField(12);
        tf4.setFont(new Font("TimesRoman", Font.PLAIN, 24));
        Box b4 = Box.createHorizontalBox();
        b4.add(label4);
        b4.add(Box.createHorizontalStrut(10));
        b4.add(tf4);
        panel5.add(b4);
        Add_Component(jfr, gbl, panel5, gbc, 0, 5, 1, 1, 1, 0);

        JLabel label5 = new JLabel("Semester         :", JLabel.CENTER);
        label5.setFont(new Font("TimesRoman", Font.PLAIN, 24));
        JTextField tf5 = new JTextField(12);
        tf5.setFont(new Font("TimesRoman", Font.PLAIN, 24));
        Box b5 = Box.createHorizontalBox();
        b5.add(label5);
        b5.add(Box.createHorizontalStrut(10));
        b5.add(tf5);
        panel6.add(b5);
        Add_Component(jfr, gbl, panel6, gbc, 0, 6, 1, 1, 1, 0);


        panel7.setBorder(BorderFactory.createEmptyBorder());
        JButton button2 = new JButton("   Confirm   ");

        button2.setBorder(BorderFactory.createRaisedBevelBorder());

        button2.setBackground(Color.LIGHT_GRAY);

        button2.setFont(new Font("TimesRoman", Font.BOLD, 30));

        JPanel emp = new JPanel();
        emp.setPreferredSize(new Dimension(700, 10));
        emp.setBorder(BorderFactory.createEmptyBorder());
        panel7.add(emp);
        panel7.add(button2, BorderLayout.CENTER);
        Add_Component(jfr, gbl, panel7, gbc, 0, 7, 1, 1, 1, 1);

        button2.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jfr.setVisible(false);

                        List<ModulesGrades> modules=json1.readArray(ModulesGrades.class);
                        modules.add(new ModulesGrades(number,tf1.getText(),tf2.getText(),tf3.getText(),tf4.getText(),tf5.getText()));
                        json1.writeArray(modules);
                        new ModulesManagement(number);
                        jfr.dispose();
                    }
                }
        );


    }
}
